package com.kylin.core.merchant.web.facade.impl;

import com.kylin.core.merchant.query.handler.MerchantTranslator;
import com.kylin.domain.merchant.command.QueryMerchantCommand;
import com.kylin.domain.merchant.provider.MerchantProvider;
import com.kylin.domain.merchant.vo.Merchant;
import com.kylin.merchant.api.dto.req.CreateMerchantReqDTO;
import com.kylin.merchant.api.dto.req.CreateShopReqDTO;
import com.kylin.merchant.api.dto.req.QueryMerchantReqDTO;
import com.kylin.merchant.api.dto.req.UpdateMerchantReqDTO;
import com.kylin.merchant.api.dto.resp.MerchantInfoRespDTO;
import com.kylin.merchant.api.dto.resp.ShopInfoRespDTO;
import com.kylin.merchant.api.web.facade.MerchantManageFacade;
import com.kylin.shared.common.anno.ArgsValid;
import com.kylin.shared.common.PageResults;
import com.kylin.shared.common.Pagination;
import com.kylin.shared.common.ResponseWrapper;
import com.kylin.shared.common.consts.Constants;
import com.kylin.shared.enums.ResultCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @author Wall.
 * @version v1.0.1
 * @date 2020年06月01日 11:06
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "商户店铺管理")
public class MerchantManageFacadeImpl implements MerchantManageFacade {
    private final MerchantProvider merchantProvider;
    private final MerchantTranslator translator;


    @ArgsValid
    @Override
    @ApiOperation(value = "获取商家列表", notes = "分页返回商家信息，每页默认10条")
    public ResponseWrapper<Pagination<MerchantInfoRespDTO>> findMerchants(QueryMerchantReqDTO queryMerchantReqDTO) {

        PageResults merchants = this.merchantProvider.findMerchants(
                QueryMerchantCommand.builder()
                        .username(queryMerchantReqDTO.getUsername())
                        .removed(queryMerchantReqDTO.getRemoved())
                        .password(queryMerchantReqDTO.getPassword())
                        .email(queryMerchantReqDTO.getEmail())
                        .qq(queryMerchantReqDTO.getQq())
                        .pageIndex(queryMerchantReqDTO.getIndex())
                        .pageSize(queryMerchantReqDTO.getLimit())
                        .build()
        );
        return new ResponseWrapper<>(new Pagination<>(
                queryMerchantReqDTO.getIndex(),
                Optional.ofNullable(queryMerchantReqDTO.getLimit()).orElse(Constants.DEFAULT_PAGE_SIZE),
                merchants.getTotal(),
                translator.toMerchantRespDTO((List<Merchant>)merchants.getResults())
        ));
    }

    @ArgsValid
    @Override
    @ApiOperation(value = "创建新商家", notes = "新建商家")
    public ResponseWrapper<Long> createMerchant(CreateMerchantReqDTO createMerchantReqDTO) {
        return null;
    }

    @ArgsValid
    @Override
    @ApiOperation(value = "修改商家信息", notes = "修改商家信息")
    public ResponseWrapper<Long> updateMerchant(UpdateMerchantReqDTO updateMerchantReqDTO) {
        return null;
    }

    @ArgsValid
    @Override
    @ApiOperation(value = "新建店铺", notes = "新建店铺信息")
    public ResponseWrapper<ShopInfoRespDTO> createShop(Long merchantId, CreateShopReqDTO createShopReqDTO) {
        if (!Objects.equals(merchantId, createShopReqDTO.getMerchantId())) {
            return new ResponseWrapper<>(ResultCodeEnum.BAD_REQUEST);
        }
        return null;
    }
}
